﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using Microsoft.Extensions.Configuration;
using MongoDB.Bson;
using MongoDB.Driver;
using MySql.Data.MySqlClient;

namespace MyFramework.DB
{
   public class MongoDBContext
    {
        private static MongoClient uniqueInstance;
        private static readonly object locker = new object();
        public static MongoClient GetInstance()
        {

            if (uniqueInstance == null)
            {
                lock (locker)
                {
                    if (uniqueInstance == null)
                    {
                        var filename = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "appsetting.json";
                        ConfigurationBuilder builder = new ConfigurationBuilder();
                        if (!File.Exists(filename))
                        {
                            throw new FileNotFoundException("配置文件不存在！");
                        }
                        builder.AddJsonFile(filename);
                        var config = builder.Build();
                        if (string.IsNullOrEmpty(config["mongoconnectionstring"]))
                        {
                            throw new SyntaxErrorException("错误的配置！");
                        }
                        uniqueInstance = new MongoClient(config["mongoconnectionstring"]);
                    }
                }


            }
            return uniqueInstance;

        }
    }
}
